package com.qf.b_servlet;

import com.mysql.jdbc.Driver;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.*;

/**
 * @author zdl
 * @date 2025/1/13
 */
@WebServlet("/test")
public class Demo1 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");
        String pwd = req.getParameter("pwd");
        try {
            boolean b = queryOne(username, pwd);
            if (b){
                resp.setContentType("text/html;charset=utf-8");
                PrintWriter out = resp.getWriter();
                out.write("<h1>"+username+"用户登录成功</h1>");
            }else {
                resp.setContentType("text/html;charset=utf-8");
                PrintWriter out = resp.getWriter();
                out.write("<h1>该"+username+"用户名不存在</h1>");
            }
        } catch (ClassNotFoundException | SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");
        String pwd = req.getParameter("pwd");
        try {
            boolean b = dbUtilQuery(username, pwd);
            if (b){
                resp.setContentType("text/html;charset=utf-8");
                PrintWriter out = resp.getWriter();
                out.write("<h1>"+username+"用户登录成功</h1>");
            }else {
                resp.setContentType("text/html;charset=utf-8");
                PrintWriter out = resp.getWriter();
                out.write("<h1>该"+username+"用户名不存在</h1>");
            }
        } catch ( SQLException e) {
            throw new RuntimeException(e);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }

    }

    public boolean queryOne(String name, String password) throws ClassNotFoundException, SQLException {
        Class.forName("com.mysql.jdbc.Driver");
        Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb1_2?useSSL=false", "root", "123456");
        String sql = "select * from tb_user where username = ? and password = ?";
        PreparedStatement ps = conn.prepareStatement(sql);
        ps.setString(1,name);
        ps.setString(2,password);
        ResultSet resultSet = ps.executeQuery();
        return  resultSet.next() ? true : false;
    }
    public boolean dbUtilQuery(String name, String password) throws SQLException, ClassNotFoundException {
        Class.forName("com.mysql.jdbc.Driver");
        Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb1_2?useSSL=false", "root", "123456");
        String sql = "select * from tb_user where username = ? and password = ?";
        QueryRunner queryRunner = new QueryRunner();
        Object[] params = {name,password};
        User query = queryRunner.query(conn, sql,params,new BeanHandler<>(User.class));
        return query != null ? true : false;
    }

}
